﻿package  {
	
	import flash.display.MovieClip;
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.filters.GlowFilter;
	import flash.events.Event;
	
	
	public class PowerUI extends MovieClip {
		
		private var _maxValue:int=100;
		private var _currentValue:int;
		private var _timer:Timer;
		//倒计时时间 单位
		private const TIMES:int=10;
		
		
		public function PowerUI() {
			// constructor code
			progressMask.scaleX=0;
			blueProgress.mask=progressMask;
			timerStart();

		}
		
		private function setProgress():void
		{
			progressMask.scaleX=_currentValue/_maxValue;
			powerText.text=_currentValue.toString();
		}
		
		public function set maxValue(maxValue:int):void
		{
			this._maxValue=maxValue;
			setProgress();
			if(_currentValue<_maxValue&&_timer==null)
				timerStart();
			else if(_currentValue>=_maxValue)
				timerEnd();
		}
		
		[Bindable(event="valueChanged")]
		public function get maxValue():int
		{
			return _maxValue;
		}
		
		public function set currentValue(currentValue:int):void
		{
			this._currentValue=currentValue;
			setProgress();
			if(_currentValue<_maxValue&&_timer==null)
				timerStart();
			else if(_currentValue>=_maxValue)
				timerEnd();
		}
		
		[Bindable(event="valueChanged")]
		public function get currentValue():int
		{
			return _currentValue;
		}
		
		private function timerStart():void
		{
			_timer=new Timer(1000,TIMES);
			_timer.addEventListener(TimerEvent.TIMER,showTime);
			_timer.addEventListener(TimerEvent.TIMER_COMPLETE,addPower);
			_timer.start();
		}
		
		private function showTime(evt:TimerEvent):void
		{
			timeText.text=clockTime(evt.target.currentCount);
		}
		
		private function addPower(evt:TimerEvent):void
		{
			if(_currentValue<_maxValue)
			{
				_currentValue+=1;
				setProgress();
			}
			timerEnd();
			if(_currentValue<_maxValue)
			{
				timerStart();
			}
			this.dispatchEvent(new Event("valueChanged"));
				
		}
		
		private function timerEnd():void
		{
			_timer.stop();
			_timer.removeEventListener(TimerEvent.TIMER,showTime);
			_timer.removeEventListener(TimerEvent.TIMER_COMPLETE,addPower);
			_timer=null;
			timeText.text="";
		}
		
		private function clockTime(seconds:int) {
			seconds=TIMES-seconds;
			var minutes:int = Math.floor(seconds/60);
			seconds -= minutes*60;
			var timeString:String = minutes+":"+String(seconds+100).substr(1,2);
			return timeString;
		}
	}
	
}
